﻿Public Class LotTransactionHelper
    Protected aLotDataRow As DataRow
    Protected aProductDataRow As DataRow
    Protected aImSpecDataRow As DataRow
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="row"></param>
    ''' <remarks></remarks>
    Public Overridable Sub AssignDefaultValue(ByVal row As DataRow)
        Dim prdId As Guid
        Dim siteId As Guid

        row("StatusID_FK") = IdRepository.APP_TRANSTATUS_PDG
        If IsDBNull(row("TranDate")) Then
            row("TranDate") = Date.Today
        End If

        If Not IsDBNull(row("LotID_FK")) Then
            aLotDataRow = SystemBoxSDK.PL.MESSDK.Lot_PL.GetLotByID(DirectCast(row("LotID_FK"), Guid))
            If aLotDataRow IsNot Nothing Then
                row("PrdId_FK") = aLotDataRow("PrdId_FK")
                row("LotTypeId_FK") = aLotDataRow("LotTypeId_FK")
                row("FROMLotStatusId_FK") = aLotDataRow("StatusID_FK")
                row("LotCD") = aLotDataRow("LotDescr_Prm")
            End If
        End If
        '
        If Not IsDBNull(row("PrdId_FK")) Then
            prdId = DirectCast(row("PrdId_FK"), Guid)
            aProductDataRow = SystemBoxSDK.PL.CoreSDK.Product_PL.GetProductByID(prdId)
            ' Logique de Default SiteSection et stock unit...
            'objectif minimiser les intervention des comis de réception.


            If aProductDataRow IsNot Nothing Then
                If Not IsDBNull(aProductDataRow("DfltWhBinID_FK")) Then
                    siteId = DirectCast(aProductDataRow("DfltWhBinID_FK"), Guid)
                    aImSpecDataRow = SystemBoxSDK.PL.MESSDK.ItemMasterSpecification_PL.GetCurrentIMS(prdId, siteId, IdRepository.STORAGE_UNIT_SPEC)
                    If aImSpecDataRow IsNot Nothing AndAlso IsDBNull(row("TranUnitId_FK")) Then
                        row("TranUnitId_FK") = aImSpecDataRow("UnitID_FK")
                    End If
                End If
            End If
        End If
    End Sub
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="row"></param>
    ''' <remarks></remarks>
    Public Overridable Sub AssignDefaultValueForAdjustement(ByVal row As DataRow)
        AssignDefaultValue(row)
    End Sub
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="row"></param>
    ''' <remarks></remarks>
    Public Overridable Sub AssignDefaultValueForRcpt(ByVal row As DataRow)
        row("TranTypeID_FK") = IdRepository.APP_TRAN_RCPT
        AssignDefaultValue(row)
    End Sub
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="row"></param>
    ''' <remarks></remarks>
    Public Overridable Sub AssignDefaultValueForTrsf(ByVal row As DataRow)
        AssignDefaultValue(row)
        row("TranTypeID_FK") = IdRepository.APP_TRAN_TRNFER
    End Sub
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="row"></param>
    ''' <remarks></remarks>
    Public Overridable Sub AssignDefaultValueForPhysCount(ByVal row As DataRow)
        AssignDefaultValue(row)
        row("TranTypeID_FK") = IdRepository.APP_TRAN_PHYCNT
        row("TOWhBinID_FK") = IdRepository.APP_SITESECTION_ADJUST_PHYCPT
    End Sub
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="row"></param>
    ''' <remarks></remarks>
    Public Overridable Sub AssignDefaultValueForPhysInv(ByVal row As DataRow)
        AssignDefaultValue(row)
        row("TranTypeID_FK") = IdRepository.APP_TRAN_PHYCNTINV
        row("TOWhBinID_FK") = IdRepository.APP_SITESECTION_ADJUST_PHYCPT
    End Sub
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="row"></param>
    ''' <remarks></remarks>
    Public Overridable Sub AssignDefaultValueForSales(ByVal row As DataRow)
        AssignDefaultValue(row)
        row("TranTypeID_FK") = IdRepository.APP_TRAN_SALES
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="view"></param>
    ''' <param name="row"></param>
    ''' <remarks></remarks>
    Public Overridable Sub AssignViewRowProperty(ByVal view As GridView, ByVal row As DataRow)

        Dim dataView As DataView = CType(view.DataSource, DataView)

        Dim AllowTranUpd As String = AppManager.GetGCCTSSecurity(IdRepository.ALLOWUPDATE, IdRepository.APP_TRAN).ToLower
        Dim AllowTranDel As String = AppManager.GetGCCTSSecurity(IdRepository.ALLOWDELETE, IdRepository.APP_TRAN).ToLower

        If row IsNot Nothing Then
            view.BeginUpdate()

            ' On ne peut pas modifier de transaction release ou Transmitted
            If Not IsDBNull(row("StatusID_FK")) AndAlso (DirectCast(row("StatusID_FK"), Guid) = IdRepository.APP_TRANSTATUS_REL Or DirectCast(row("StatusID_FK"), Guid) = IdRepository.APP_TRANSTATUS_TRANS) Then
                LockColumns(view)
            Else
                'On assigne les securités sur la ligne
                If Not IsDBNull(row("TranTypeID_FK")) Then
                    If Not AllowTranDel.Contains(row("TranTypeID_FK").ToString) Then
                        dataView.AllowDelete = False
                    Else
                        dataView.AllowDelete = True
                    End If
                    If Not AllowTranUpd.Contains(row("TranTypeID_FK").ToString) Then
                        dataView.AllowEdit = False
                        LockColumns(view)
                    Else
                        dataView.AllowEdit = True
                        view.Columns("TranTypeID_FK").OptionsColumn.AllowEdit = True
                    End If

                    ' Selon le LotTran Type on débarre les colonnes.
                    If dataView.AllowEdit Then
                        LockColumns(view)
                        EnableReadOnly(view)
                        Select Case DirectCast(row("TranTypeID_FK"), Guid)
                            Case IdRepository.APP_TRAN_RCPT
                                PrepareColumnForReceipt(view)
                            Case IdRepository.APP_TRAN_ADJMNT
                                PrepareColumnForAdjmnt(view)
                            Case IdRepository.APP_TRAN_PHYCNT, IdRepository.APP_TRAN_PHYCNTINV
                                PrepareColumnForPhysCount(view)
                            Case IdRepository.APP_TRAN_SALES
                                PrepareColumnForSales(view)
                            Case IdRepository.APP_TRAN_TRNFER
                                PrepareColumnForTrsf(view)
                            Case IdRepository.APP_TRAN_RTRNTOSTOCK
                                PrepareColumnForReturnToStock(view)
                            Case Else
                        End Select
                    End If
                End If
            End If
            view.EndUpdate()
        End If
    End Sub

  

    Public Overridable Sub EnableReadOnly(ByVal view As GridView)
        For Each col As GridColumn In view.Columns
            col.OptionsColumn.ReadOnly = False
        Next
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="row"></param>
    ''' <remarks></remarks>
    Public Overridable Sub InitRow(ByVal row As DataRow)
        row("TranDate") = DBNull.Value
        'row("PrdId_FK") = DBNull.Value
        'row("LotID_FK") = DBNull.Value
        row("LotCD") = DBNull.Value
        row("FROMLotStatusId_FK") = DBNull.Value
        row("TOLotStatusId_FK") = DBNull.Value
        row("Qty") = DBNull.Value
        row("TranUnitId_FK") = DBNull.Value
        row("StatusID_FK") = DBNull.Value
        row("Comment") = DBNull.Value
        row("LotTypeId_FK") = DBNull.Value
        row("RefDoc") = DBNull.Value
        row("FROMWhBinID_FK") = DBNull.Value
        row("TOWhBinID_FK") = DBNull.Value

        row("Crtd_DT") = Date.Now
        row("Crtd_UId") = AppManager.UserName
        row("LUpd_DT") = Date.Now
        row("LUpd_UId") = AppManager.UserName
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="view"></param>
    ''' <remarks></remarks>
    Public Overridable Sub PrepareColumnForAdjmnt(ByVal view As GridView)
        view.Columns("TranTypeID_FK").OptionsColumn.AllowEdit = True
        view.Columns("TranDate").OptionsColumn.AllowEdit = True
        view.Columns("LotID_FK").OptionsColumn.AllowEdit = True
        view.Columns("FROMWhBinID_FK").OptionsColumn.AllowEdit = True
        view.Columns("TOWhBinID_FK").OptionsColumn.AllowEdit = True
        view.Columns("Qty").OptionsColumn.AllowEdit = True
        view.Columns("RefDoc").OptionsColumn.AllowEdit = True
        view.Columns("Comment").OptionsColumn.AllowEdit = True
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="view"></param>
    ''' <remarks></remarks>
    Public Overridable Sub PrepareColumnForPhysCount(ByVal view As GridView)
        view.Columns("TranTypeID_FK").OptionsColumn.AllowEdit = True
        view.Columns("TranDate").OptionsColumn.AllowEdit = True
        view.Columns("LotID_FK").OptionsColumn.AllowEdit = True
        view.Columns("FROMWhBinID_FK").OptionsColumn.AllowEdit = True
        view.Columns("TOWhBinID_FK").OptionsColumn.AllowEdit = True
        view.Columns("Qty").OptionsColumn.AllowEdit = True
        view.Columns("RefDoc").OptionsColumn.AllowEdit = True
        view.Columns("Comment").OptionsColumn.AllowEdit = True
    End Sub
    ''' <summary>
    ''' This is to prepare colums of a Return to Stock
    ''' </summary>
    ''' <param name="view"></param>
    ''' <remarks>
    ''' This transaction type is allowed only ofr SCIEF devepper and Archictech for testing in open loop
    ''' Any Group of User shall not have access directly
    ''' Only User Group of ProdOrder Form should be granted access to that Transaction type via GCCTS list
    ''' </remarks>
    Private Sub PrepareColumnForReturnToStock(view As GridView)
        view.Columns("TranTypeID_FK").OptionsColumn.AllowEdit = True
        view.Columns("TranDate").OptionsColumn.AllowEdit = True
        view.Columns("LotID_FK").OptionsColumn.AllowEdit = True
        view.Columns("FROMWhBinID_FK").OptionsColumn.AllowEdit = True
        view.Columns("TOWhBinID_FK").OptionsColumn.AllowEdit = True
        view.Columns("Qty").OptionsColumn.AllowEdit = True
        view.Columns("RefDoc").OptionsColumn.AllowEdit = True
        view.Columns("Comment").OptionsColumn.AllowEdit = True
    End Sub
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="view"></param>
    ''' <remarks></remarks>
    Public Overridable Sub PrepareColumnForTrsf(ByVal view As GridView)
        view.Columns("TranTypeID_FK").OptionsColumn.AllowEdit = True
        view.Columns("TranDate").OptionsColumn.AllowEdit = True
        view.Columns("LotID_FK").OptionsColumn.AllowEdit = True
        view.Columns("FROMWhBinID_FK").OptionsColumn.AllowEdit = True
        view.Columns("TOWhBinID_FK").OptionsColumn.AllowEdit = True
        view.Columns("Qty").OptionsColumn.AllowEdit = True
        view.Columns("RefDoc").OptionsColumn.AllowEdit = True
        view.Columns("Comment").OptionsColumn.AllowEdit = True
    End Sub
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="view"></param>
    ''' <remarks></remarks>
    Public Overridable Sub PrepareColumnForSales(ByVal view As GridView)
        view.Columns("TranTypeID_FK").OptionsColumn.AllowEdit = True
        view.Columns("TranDate").OptionsColumn.AllowEdit = True
        view.Columns("LotID_FK").OptionsColumn.AllowEdit = True
        view.Columns("FROMWhBinID_FK").OptionsColumn.AllowEdit = True
        view.Columns("TOWhBinID_FK").OptionsColumn.AllowEdit = False
        view.Columns("Qty").OptionsColumn.AllowEdit = True
        view.Columns("RefDoc").OptionsColumn.AllowEdit = True
        view.Columns("Comment").OptionsColumn.AllowEdit = True
    End Sub
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="view"></param>
    ''' <remarks></remarks>
    Public Overridable Sub PrepareColumnForReceipt(ByVal view As GridView)
        Dim aRow As DataRow = view.GetFocusedDataRow()

        'C'est Quand on descend 
        ' le validateRow a déjà Lever
        view.Columns("TranTypeID_FK").OptionsColumn.AllowEdit = True
        view.Columns("TranDate").OptionsColumn.AllowEdit = True


        If Not IsDBNull(aRow("LotID_FK")) Then
            view.Columns("LotID_FK").OptionsColumn.AllowEdit = True
            view.Columns("LotTypeId_FK").OptionsColumn.AllowEdit = False
            view.Columns("LotCD").OptionsColumn.AllowEdit = False
            view.Columns("PrdId_FK").OptionsColumn.AllowEdit = False
        Else
            view.Columns("LotTypeId_FK").OptionsColumn.AllowEdit = True
            view.Columns("LotCD").OptionsColumn.AllowEdit = True
            view.Columns("PrdId_FK").OptionsColumn.AllowEdit = True
        End If

        If IsDBNull(aRow("PrdId_FK")) AndAlso IsDBNull(aRow("LotCD")) AndAlso IsDBNull(aRow("LotID_FK")) Then
            view.Columns("LotID_FK").OptionsColumn.AllowEdit = True
            view.Columns("LotTypeId_FK").OptionsColumn.AllowEdit = True
            view.Columns("PrdId_FK").OptionsColumn.AllowEdit = True
            view.Columns("LotCD").OptionsColumn.AllowEdit = True
        End If

        If Not IsDBNull(aRow("PrdId_FK")) AndAlso IsDBNull(aRow("LotCD")) Then
            view.Columns("LotCD").OptionsColumn.AllowEdit = True
        End If

        If Not IsDBNull(aRow("LotCD")) AndAlso IsDBNull(aRow("PrdId_FK")) Then
            view.Columns("PrdId_FK").OptionsColumn.AllowEdit = True
        End If

        view.Columns("TOWhBinID_FK").OptionsColumn.AllowEdit = True
        view.Columns("Qty").OptionsColumn.AllowEdit = True

        'En commentaire car on prend les storage unit du Produit
        'view.Columns("TranUnitId_FK").OptionsColumn.AllowEdit = True

        view.Columns("RefDoc").OptionsColumn.AllowEdit = True
        view.Columns("Comment").OptionsColumn.AllowEdit = True
        '
        view.Columns("SupplierLotNo").OptionsColumn.AllowEdit = True
        view.Columns("Carrier").OptionsColumn.AllowEdit = True
        view.Columns("BillOfLading").OptionsColumn.AllowEdit = True
        view.Columns("PackingSlipNo").OptionsColumn.AllowEdit = True
        '
        view.Columns("COA").OptionsColumn.AllowEdit = True
        view.Columns("ExpiryDate").OptionsColumn.AllowEdit = True
    End Sub


    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Overridable Sub ProcessBatchTransactionWSvc(ByVal sender As Object, ByVal e As EventArgs)
        Me.Cursor = Cursors.WaitCursor
        Dim SysBoxSvc As SystemBoxWSvc = New SystemBoxWSvc()
        'Attention: Cette procedure doit toujours etre utilise a partir d'une action d'une grille détail (GridSelectedRows)
        Dim aGridSelectedRows As GridSelectedRows = CType(CType(sender, DXMenuItem).Tag, GridSelectedRows)
        Dim aDataRow As DataRow = aGridSelectedRows.CurrentSelectedDataRow
        Try
            SysBoxSvc.ProcessBatchTransaction(aDataRow)
        Catch ex As Exception
            MessageWindow.ShowMessage(ex.Message, MsgBoxStyle.Information)
        End Try
        Me.Cursor = Cursors.Default
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Overridable Sub ProcessPOTransactionWSvc(ByVal sender As Object, ByVal e As EventArgs)
        Me.Cursor = Cursors.WaitCursor
        Dim SysBoxSvc As SystemBoxWSvc = New SystemBoxWSvc()
        'Attention: Cette procedure doit toujours etre utilise a partir d'une action d'une grille détail (GridSelectedRows)
        Dim aGridSelectedRows As GridSelectedRows = CType(CType(sender, DXMenuItem).Tag, GridSelectedRows)
        Dim aDataRow As DataRow = aGridSelectedRows.CurrentSelectedDataRow
        Try
            SysBoxSvc.ProcessPOTransaction(aDataRow)
        Catch ex As Exception
            MessageWindow.ShowMessage(ex.Message, MsgBoxStyle.Information)
        End Try
        Me.Cursor = Cursors.Default
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Overridable Sub ProcessPOWSvc(ByVal sender As Object, ByVal e As EventArgs)
        Me.Cursor = Cursors.WaitCursor
        Dim SysBoxSvc As SystemBoxWSvc = New SystemBoxWSvc()
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Try
            SysBoxSvc.ProcessPO(aDataRow)
        Catch ex As Exception
            MessageWindow.ShowMessage(ex.Message, MsgBoxStyle.Information)
        End Try
        Me.Cursor = Cursors.Default
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Overridable Sub ProcessBatchTransaction(ByVal sender As Object, ByVal e As EventArgs)
        Me.Cursor = Cursors.WaitCursor
        'Attention: Cette procedure doit toujours etre utilise a partir d'une action d'une grille détail (GridSelectedRows)
        Dim aGridSelectedRows As GridSelectedRows = CType(CType(sender, DXMenuItem).Tag, GridSelectedRows)
        Dim aDataRow As DataRow = aGridSelectedRows.CurrentSelectedDataRow
        Try
            LotTransaction_PL.ProcessBatchTransaction(aDataRow("lotTranID"))
        Catch ex As Exception
            MessageWindow.ShowMessage(ex.Message, MsgBoxStyle.Information)
        End Try
        Me.Cursor = Cursors.Default
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Overridable Sub ProcessPOTransaction(ByVal sender As Object, ByVal e As EventArgs)
        Me.Cursor = Cursors.WaitCursor
        'Attention: Cette procedure doit toujours etre utilise a partir d'une action d'une grille détail (GridSelectedRows)
        Dim aGridSelectedRows As GridSelectedRows = CType(CType(sender, DXMenuItem).Tag, GridSelectedRows)
        Dim aDataRow As DataRow = aGridSelectedRows.CurrentSelectedDataRow
        Try
            LotTransaction_PL.ProcessBatchTransaction(aDataRow("lotTranID"))
        Catch ex As Exception
            MessageWindow.ShowMessage(ex.Message, MsgBoxStyle.Information)
        End Try
        Me.Cursor = Cursors.Default
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Overridable Sub ProcessPO(ByVal sender As Object, ByVal e As EventArgs)
        Me.Cursor = Cursors.WaitCursor
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Try
            InventoryBatchs_PL.ProcessInventoryBatch(aDataRow("invBatchID"))
        Catch ex As Exception
            MessageWindow.ShowMessage(ex.Message, MsgBoxStyle.Information)
        End Try
        Me.Cursor = Cursors.Default
    End Sub





    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="view"></param>
    ''' <remarks></remarks>
    Public Overridable Sub LockColumns(ByVal view As GridView)
        For Each col As GridColumn In view.Columns
            col.OptionsColumn.AllowEdit = False
        Next
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="view"></param>
    ''' <remarks>
    ''' 'OutStandingIssue: Devrait rependre les valeurs de lock de la metaquery - aleblanc.
    ''' 
    ''' 
    ''' </remarks>    
    Public Overridable Sub UnLockColumns(ByVal view As GridView)
        For Each col As GridColumn In view.Columns
            col.OptionsColumn.AllowEdit = True
        Next
    End Sub
End Class
